<template>
   
  <el-card class="form-container" shadow="never">
    <el-form
      :model="homeBanner"
      :rules="rules"
      ref="homeBannerFrom"
      label-width="150px"
      size="small"
    >
      <el-form-item label="轮播图标题：" prop="title">
        <el-input v-model="homeBanner.title" class="input-width"></el-input>
      </el-form-item>
      <el-form-item label="轮播图位置：" prop="type">
        <el-select v-model="homeBanner.type">
          <el-option
            v-for="type in typeOptions"
            :key="type.value"
            :label="type.label"
            :value="type.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="开始时间：">
        <el-date-picker
          type="datetime"
          placeholder="选择日期"
          v-model="homeBanner.startTime"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="到期时间：">
        <el-date-picker
          type="datetime"
          placeholder="选择日期"
          v-model="homeBanner.endTime"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="上线/下线：">
        <el-radio-group v-model="homeBanner.status">
          <el-radio :label="0">下线</el-radio>
          <el-radio :label="1">上线</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="图片：">
        <single-upload v-model="homeBanner.cover"></single-upload>
      </el-form-item>
      <el-form-item label="排序：">
        <el-input v-model="homeBanner.sort" class="input-width"></el-input>
      </el-form-item>
      <el-form-item label="链接：" prop="link">
        <el-input v-model="homeBanner.link" class="input-width"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('homeBannerFrom')"
          >提交</el-button
        >
        <el-button v-if="!isEdit" @click="resetForm('homeBannerFrom')"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
  </el-card>
</template>
<script>
import SingleUpload from "@/components/Upload/singleUpload";
import {
  createHomeBanner,
  getHomeBanner,
  updateHomeBanner,
} from "@/api/banner";
const defaultTypeOptions = [
  {
    label: "PC首页轮播",
    value: 0,
  },
];
const defaultHomeBanner = {
  title: null,
  type: 0,
  cover: null,
  startTime: null,
  endTime: null,
  status: 0,
  link: null,
  sort: 0,
};
export default {
  name: "HomeBannerDetail",
  components: { SingleUpload },
  props: {
    isEdit: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      homeBanner: Object.assign({}, defaultHomeBanner),
      rules: {
        title: [
          { required: true, message: "请输入轮播图标题", trigger: "blur" },
          {
            min: 2,
            max: 140,
            message: "长度在 2 到 140 个字符",
            trigger: "blur",
          },
        ],
        type: { required: true, message: "请选择轮播图位置", trigger: "blur" },
        link: [{ required: true, message: "请输入轮播图链接", trigger: "blur" }],
        cover: [{ required: true, message: "请选择图片", trigger: "blur" }],
      },
      typeOptions: Object.assign({}, defaultTypeOptions),
    };
  },
  created() {
    if (this.isEdit) {
      getHomeBanner(this.$route.query.id).then((response) => {
        this.homeBanner = response.data;
      });
    } else {
      this.homeBanner = Object.assign({}, defaultHomeBanner);
    }
  },
  methods: {
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$confirm("是否提交数据", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }).then(() => {
            if (this.isEdit) {
              updateHomeBanner(this.$route.query.id, this.homeBanner).then(
                (response) => {
                  this.$refs[formName].resetFields();
                  this.$message({
                    message: "修改成功",
                    type: "success",
                    duration: 1000,
                  });
                  this.$router.back();
                }
              );
            } else {
              createHomeBanner(this.homeBanner).then((response) => {
                this.$refs[formName].resetFields();
                this.homeBanner = Object.assign({}, defaultHomeBanner);
                this.$message({
                  message: "提交成功",
                  type: "success",
                  duration: 1000,
                });
              });
            }
          });
        } else {
          this.$message({
            message: "验证失败",
            type: "error",
            duration: 1000,
          });
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.homeBanner = Object.assign({}, defaultHomeBanner);
    },
  },
};
</script>
<style scoped>
.input-width {
  width: 70%;
}
</style>


